import math
import time

import pydirectinput as pdg


def calculate_angle(pointA, pointB, pointC, pointD):
    x1, y1 = pointA[0], pointA[1]
    x2, y2 = pointB[0], pointB[1]
    x3, y3 = pointC[0], pointC[1]
    x4, y4 = pointD[0], pointD[1]

    # 计算 AB 和 CD 的单位向量 u 和 v
    vectorU = (x2 - x1, y2 - y1)
    vectorV = (x4 - x3, y4 - y3)

    magnitudeU = math.sqrt((vectorU[0]) ** 2 + (vectorU[1]) ** 2)
    magnitudeV = math.sqrt((vectorV[0]) ** 2 + (vectorV[1]) ** 2)

    dotProduct = vectorU[0] * vectorV[0] + vectorU[1] * vectorV[1]
    cosineAngle = dotProduct / (magnitudeU * magnitudeV)

    angleRadian = math.acos(cosineAngle)
    angleDegree = math.degrees(angleRadian)

    return angleDegree


def calculate_angle_gpt(line1, line2):
    # 计算两条直线的斜率
    vector_AB = [line1[1][0] - line1[0][0], line1[1][1] - line1[0][1]]
    vector_CD = [line2[1][0] - line2[0][0], line2[1][1] - line2[0][1]]

    # 计算向量AB和向量CD的点积
    dot_product = vector_AB[0] * vector_CD[0] + vector_AB[1] * vector_CD[1]

    # 计算向量AB和向量CD的模
    magnitude_AB = math.sqrt(vector_AB[0] ** 2 + vector_AB[1] ** 2)
    magnitude_CD = math.sqrt(vector_CD[0] ** 2 + vector_CD[1] ** 2)

    # 计算夹角的弧度
    angle_rad = math.acos(dot_product / (magnitude_AB * magnitude_CD))

    # 将弧度转换为角度
    angle_degrees = math.degrees(angle_rad)

    return angle_degrees


def click():
    pdg.mouseDown()
    pdg.mouseUp()


screen_center_x = int(1280 / 2)
screen_center_y = int(960 / 2)
foot_center_y = 620


def test():
    pdg.moveTo(300, 300)
    pdg.click()
    time.sleep(2)

    pos1 = (189, 545)
    pos2 = (186, 550)
    pos3 = (198, 556)


def move_1():
    point_1 = (screen_center_x - 400, foot_center_y)
    # point_1 = (screen_center_x - 100, screen_center_y)
    pdg.moveTo(*point_1)
    click()


def move_2():
    point = (screen_center_x, screen_center_y - 200)
    pdg.moveTo(*point)
    click()


def test_cal_angle():
    pointA = [187, 547]
    pointB = [198, 554]
    pointC = pointA
    pointD = [187, 554]
    line1 = [pointA, pointB]
    line2 = [pointC, pointD]

    result = calculate_angle(pointA, pointB, pointC, pointD)
    result_gpt = calculate_angle_gpt(line1, line2)
    print("直线夹角为：", result)
    print("直线夹角为：", result_gpt)
    #
    # X0 = 258
    # Y0 = 584
    #
    # X1 = 249
    # Y1 = 581
    X = 1
    Y = 2
    Z = 3
    coor_3d = (X, Y, Z)

    # 二维透视
    x = 1
    y = 1
    coor_2d = x, y

    # Camera位置
    Cx = 1
    Cy = 1
    Cz = 1
    coor_camera = (Cx, Cy, Cz)

    D = 10
    x = (D * X) / (Z - Cz)
    y = (D * Y) / (Z - Cz)


def cal_near_spot(point, target_point, expected_distance=6):
    Xp, Yp = point
    Xt, Yt = target_point
    # print("(Xp - Xt)^2:", (Xp - Xt)**2)
    distance = math.sqrt((Xp - Xt) ** 2 + (Yp - Yt) ** 2)
    print("distance:", distance)
    ratio = expected_distance / distance
    Xe = Xp + ratio * (Xt - Xp)
    Ye = Yp + ratio * (Yt - Yp)
    return (Xe, Ye)


def dynamic_search():
    pass


def test_angle():
    # 经过测试，需要按8下，90度，那么
    pdg.moveTo(300, 300)
    pdg.click()
    time.sleep(1)
    pdg.press("left")


def cal_turn(point, next_move_point, target_point):
    coor = cal_near_spot(point, target_point)

    line1 = [next_move_point, point]
    line2 = [next_move_point, target_point]
    angle = calculate_angle_gpt(line1, line2)
    print("coor:", coor)
    print("angle:", angle)
    angle_per_turn = 90 / 8
    times_to_turn = (180 - angle) / angle_per_turn
    print(times_to_turn)
    return round(times_to_turn)


def turn(round=9, sleep=None):
    pdg.moveTo(300, 300)
    pdg.click()
    for i in range(round):
        pdg.press("left")
        if sleep is not None:
            time.sleep(1)


def get_euclidean_distance(point1, point2):
    distance = math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
    return distance


def check_in_range(point, target_point, criterion=3):
    distance = get_euclidean_distance(point, target_point)
    print("distance:", distance)
    if distance <= criterion:
        return True
    else:
        return False

def move_mouse():
    pdg.moveTo(300,300)
    pdg.click()
    while True:
        pdg.press("f2")
        time.sleep(60)
        # pdg.press("f1")
        # time.sleep(60)


if __name__ == "__main__":
    # move_1()
    # move_2()
    # 测试样例
    # 三个点：
    # 1.世界坐标：世界的客观坐标，就是屏幕上的X和Z，之所以是Z，因为这里设定Y是垂直于地面的；
    # 2.相机坐标：观察出发点，固定相机之后，Y轴被固定了；
    # 3.屏幕坐标：像素
    # X, Y, Z = [187, 547, 0]
    # Cx, Cy, Cz = (1, 1, 1)

    # 计算

    # target_point = (198, 540)
    # point = (189, 549)
    # #
    # next_move_point = (182, 552)
    # # round = cal_turn(point, next_move_point, target_point)
    # # turn(round=round)
    # print("下一次动作",get_euclidean_distance(next_move_point, target_point))
    # print(get_euclidean_distance(point, target_point))
    # result = (191, 546)
    # r = check_in_range(result, target_point)
    # print(r)

    # turn(round=9)
    move_mouse()